package com.zz.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String url;
    private static String driver;
    private static String username;
    private static String password;

    static{
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/jdbc.properties"));
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            Class.forName(driver);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }

    public static PreparedStatement pre(Connection con, String sql){
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ps;
    }

    public static int preExe(PreparedStatement ps){
        int num = 0;
        try {
            num = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    public static int preExe(PreparedStatement ps, Object...objs){
        int num = 0;
        try {
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i+1, objs[i]);
            }
            num = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    public static Object preQuery(PreparedStatement ps){
        Object obj = null;
        try {
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                Date borndate = rs.getDate("borndate");
                String phone = rs.getString("phone");
                System.out.println("id = " + id);
                System.out.println("name = " + name);
                System.out.println("sex = " + sex);
                System.out.println("borndate = " + borndate);
                System.out.println("phone = " + phone);
                System.out.println("");
            }
            rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return obj;
    }

    public static void close(PreparedStatement ps, Connection con){
        close(ps);
        close(con);
    }

    public static void close(PreparedStatement ps){
        try {
            ps.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void close(Connection con){
        try {
            con.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
